#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxd = 19;
const int MOD = 2520;
int A[maxd];
ll d[maxd+2][MOD+10][55];
ll l, r;

int cnt = 0;
map<int, int> ID;
vector<int> cache;

inline int LCM(int a, int b){
    if(a == 0) return b;
    if(b == 0) return a;
    return 1ll*a*b/__gcd(a,b);
}

void find(){
    cache.push_back(0);
    ID[0] = cnt++;
    for (int i = 1; i < 1 << 9; ++i) {
        int lcm = 0;
        for (int p = 1; p <= 9; ++p) if(i & (1<<(p-1))){
            lcm = LCM(lcm, p);
        }
        if(!ID.count(lcm)) {
            cache.push_back(lcm);
            ID[lcm] = cnt++;
        }
    }
}


ll dp(int pos, int pre, int id, bool flg){
    int lcm = cache[id];
    if(pos == 0) return (lcm && pre%lcm==0);
    if(flg && d[pos][pre][id]!=-1) return d[pos][pre][id];
    int m = flg?9:A[pos];
    ll ans = 0;
    for (int i = 0; i <= m; ++i) {
        if(i){
            lcm = LCM(i, cache[id]);
        }
        ans += dp(pos-1, (pre*10+i)%MOD, ID[lcm], flg||i<m);
    }
    if(flg) d[pos][pre][id] = ans;
    return ans;
}

ll cal(ll x){
    if(x==0) return 0;
    int pos = 0;
    do{
        A[++pos]=x%10;
        x/=10;
    }while(x);
    return dp(pos, 0, 0, false);
}

int t;
signed main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    find();
//    for(auto i:cache) cout << i << endl;
//    cout << "size " << cache.size();
    memset(d, -1, sizeof(d));
    int T; cin >> T;
    for (int i = 1; i <= T; ++i) {
        cin >>l >> r;
//        t = clock();
        cout <<cal(r) - cal(l-1)<< endl;
//        cout << (double)(clock()-t)/CLOCKS_PER_SEC << endl;
    }

    return 0;
}